Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Text
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls

<ToolboxData("<{0}:State runat=server></{0}:State>")> _
Public Class State
    Inherits WebControl
    Private bk As Book

    <Serializable()> _
        Protected Structure StateData
        Dim strCustNumber As String
        Dim intCustStatus As Integer
    End Structure

    Private sData As New StateData

    Private Sub State_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.TrackViewState()
        If Me.IsViewStateEnabled = True Then
            If Me.IsTrackingViewState = False Then
                Me.TrackViewState()
            End If
            Me.ViewState("custId") = "A123"
        End If

        If Me.HasChildViewState = True And _
           Me.IsChildControlStateCleared = False Then
            Me.ClearChildState()
        ElseIf Me.HasChildViewState = True Then
            Me.ClearChildViewState()
        ElseIf Me.IsChildControlStateCleared = False Then
            Me.ClearChildControlState()
        End If

        If Me.Page.RequiresControlState(Me) = False Then
            Me.Page.RegisterRequiresControlState(Me)
        End If
    End Sub

    Protected Overrides ReadOnly Property ViewStateIgnoresCase() As Boolean

        Get
            Return False
        End Get

    End Property

    Protected Overrides Function SaveViewState() As Object
        'sData.intCustStatus = 9
        'sData.strCustNumber = "A49K72"
        'Return sData

        Dim tcv As New BookToString
        Dim strSerializedBook As String
        strSerializedBook = tcv.ConvertFrom(Nothing, Nothing, bk)

        Dim tcv2 As System.ComponentModel.TypeConverter
        tcv2 = TypeDescriptor.GetConverter(bk)
        strSerializedBook = tcv2.ConvertFrom(Nothing, Nothing, bk)

        Return strSerializedBook



    End Function

    Protected Overrides Sub LoadViewState(ByVal savedState As Object)
        Dim tcv As BookToString
        Dim objDeserialized As Object

        sData = CType(savedState, StateData)
        tcv = New BookToString
        objDeserialized = tcv.ConvertTo(Nothing, Nothing, savedState, Nothing)
        bk = CType(objDeserialized, Book)

    End Sub

    Public Function GetBook(ByVal strISBN As String) As Book
        Dim TwentyMinutes As TimeSpan
        Dim strBookKey As String


        strBookKey = "Book" & strISBN & Me.Page.Session.SessionID

        If Me.Page.Cache(strBookKey) Is Nothing Then
            bk = New Book(strISBN)
            TwentyMinutes = New TimeSpan(0, 20, 0)
            Me.Page.Cache.Add(strBookKey, bk, Nothing, DateTime.MaxValue, TwentyMinutes, _
               Web.Caching.CacheItemPriority.Normal, Nothing)
            Me.ViewState("BookKey") = strISBN
        Else
            bk = Me.Page.Cache(strBookKey)
        End If
        Return bk
    End Function

End Class
